package com.wms.service;

import com.mes.entity.User;
import com.yy.base.common.dao.Dao;
import com.yy.base.common.service.BaseService;
import com.yy.base.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

@Service
public class BuyStorageService extends BaseService {
	@Autowired
	private Dao dao;
	/**
	 * 加载楦头信息
	 * @return
	 */
	public List<Map<String, Object>> getXuantouList() {
		String sql = " select xt.code as XTCODE, xt.name as XTNAME , xt.emes_xuantou_id as XTID\n" +
				" from Emes_Xuantou xt ";
		return dao.query(sql);
	}

	/**
	 * 加载夹板物料信息
	 * @return
	 */
	public List<Map<String, Object>> getProductJbList() {
		String sql = " select wl.VALUE as WLCODE ,wl.name as WLNAME , wl.m_product_id as WLID\n" +
				"from m_product wl ,m_product_category fl\n" +
				"where  wl.m_product_category_id = fl.m_product_category_id\n" +
				"and fl.value = 'JB' ";
		return dao.query(sql);
	}

	/**
	 * 加载夹板物料信息
	 * @return
	 */
	public List<Map<String, Object>> getProductYdmList() {
		String sql = " select wl.VALUE as WLCODE ,wl.name as WLNAME , wl.m_product_id as WLID\n" +
				"from m_product wl ,m_product_category fl\n" +
				"where  wl.m_product_category_id = fl.m_product_category_id\n" +
				"and fl.value = 'SC_YDM' ";
		return dao.query(sql);
	}

	/**
	 * 加载尺码信息
	 * @return
	 */
	public List<Map<String, Object>> getSizeList() {
		String sql = " select cm.code as CMCODE ,cm.emes_size_id as CMID from emes_size cm order by cm.code asc  ";
		return dao.query(sql);
	}

	/**
	 * 根据对应的楦头系列加载楦型
	 * @return
	 */
	public List<Map<String, Object>> getProductList(String xxid) {
		String sql = " select  xx.emes_xuantou_model_id as XXID , xx.type as XXTYPE , xx.m_product_id as WLID from Emes_Xuantou_Model xx where xx.emes_xuantou_id = ? ";

		return dao.query(sql,xxid);
	}

	/**
	 * 根据对应的物料ID和尺码ID找到对应的batch表ID
	 * @return
	 */
	public List<Map<String, Object>> getBatchList(String sizeId, String wlid) {
		String sql = "select u.wms_storage_batch_id as BATCHID from wms_storage_batch u where  u.emes_size_id = '"+sizeId+"' and u.m_product_id =  '"+wlid+"' ";


		return dao.query(sql);
	}

	/**
	 * 根据对应的物料ID找出所属物料分类
	 * @return
	 */
	public List<Map<String, Object>> getProductFlList(String wlid) {
		String sql = " SELECT   " +
				"   fl.value  as FLTYPE , wl.c_uom_id as UOMID ,wl.value as  WLVALUE " +
				"FROM   " +
				"   m_product wl,   " +
				"   m_product_category fl    " +
				"WHERE   " +
				"   wl.m_product_category_id = fl.m_product_category_id   " +
				"and wl.m_product_id = '"+wlid+"'    ";
		return dao.query(sql);
	}

	/**
	 * 根据录入的夹板RFID找ITEM表中，如已存在则不在录入
	 * @return
	 */
	public List<Map<String, Object>> getProductJb(String wlid) {
		String sql = " select * from wms_storage_batch_item where  code = ? ";
		return dao.query(sql,wlid);
	}

	/**
	 * 往批次表中插入信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public boolean inStorageBatch(String uuid ,String size, String uomid, String pid, User user) {
		boolean result = false;


		String sql = "insert into Wms_Storage_Batch(Wms_Storage_Batch_id,Emes_Size_Id,Status,C_Uom_ID,In_time,Is_Self,M_Product_ID,Batch_No," +
				"AD_CLIENT_ID, AD_ORG_ID,ISACTIVE, CREATED, CREATEDBY, UPDATED, UPDATEDBY)" +
				" values(?,?,'正常',?,now(),'Y',?,?,?,?,?,now(),?,now(),?)";

		result = dao.executeSql(sql, uuid,size,  uomid, pid, "", user.getClientId(), user.getOrgId(), "Y", user.getId(), user.getId()) > 0;

		return result;
	}


	/**
	 * 往inout表中插入信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public boolean inStorageInOut(String uuid ,String warehouseID, int qty, String uomID , User user) {
		boolean result = false;
		String uuids = StringUtil.getUUID();

		String sql = "insert into wms_storage_in_out "
				+ " (Wms_Storage_In_Out_ID,Wms_Storage_Batch_ID,M_Warehouse_ID,Qty,C_Uom_ID,Opt_Mode,Status,"
				+ "AD_CLIENT_ID, AD_ORG_ID,ISACTIVE, CREATED, CREATEDBY, UPDATED, UPDATEDBY)" +
				" values(?,?,?,?,?,?,'OK',?,?,?,now(),?,now(),?)";


		result =  dao.executeSql(sql, uuids,uuid,warehouseID,qty,uomID, "采购入库", user.getClientId(), user.getOrgId(), "Y", user.getId(), user.getId()) > 0;

		return result;
	}

	/**
	 * 往batch_item表中插入信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public boolean inStorageItem(String uuid ,String type, String code , User user,String foottype) {
		boolean result = false;
		String uuids = StringUtil.getUUID();

		String sql = "insert into Wms_Storage_Batch_Item "
				+ " (Wms_Storage_Batch_Item_ID,Wms_Storage_Batch_ID,Type,Code, "
				+ " AD_CLIENT_ID, AD_ORG_ID,ISACTIVE, CREATED, CREATEDBY, UPDATED, UPDATEDBY,foot_type)" +
				" values(?,?,?,?,?,?,?,now(),?,now(),?,?)";


		result =  dao.executeSql(sql, uuids,uuid,type,code, user.getClientId(), user.getOrgId(), "Y", user.getId(), user.getId(),foottype) > 0;

		return result;
	}



}